/**
 * Created by huotaol on 2017/5/16.
 */
import {Alert, Linking} from "react-native";
import React from "react";
import Toast from "react-native-root-toast";


export const OFFICIAL_PHONE_NUMBER = '400-6161-677';

export function toast(msg) {
    Toast.show(msg, {
        duration: Toast.durations.SHORT,
        position: Toast.positions.CENTER
    });
}

export function isArray(o) {
    return Object.prototype.toString.call(o) === '[object Array]';
}

export function callPhone(phone) {
    if (!phone) {
        return;
    }
    const url = 'tel:' + phone;
    open(url);
}

export function openURL(url) {
    open(url);
}

// Linking支持如下功能
// url='http://www.lcode.org' 打开http网页
// url='https://www.baidu.com' 打开https网页
// url='smsto:18352402477'  发送短信
// url='tel:18352402477' 打电话
// url='mailto:jiangqqlmj@163.com' 发邮件
function open(url) {
    Linking.canOpenURL(url).then(
        support => {
            if (support) {
                Linking.openURL(url).catch(err => console.error('An error occurred', err));
            } else {
                Alert.alert('错误', '系统不支持此功能');
            }
        }
    ).catch(
        err => {
            Alert.alert('错误', err);
        }
    );
}